clear all
set more off
cap log close
cap ssc install sxpose

do "/Users/alisatns/Dropbox/Projects/Canada - Capital Gains/RESTAT Replication Package/Do files/0_Set_Directories.do"

****************************************************************************
* This do-file 
* 
* Prepares graphs
* 
****************************************************************************

set scheme stmono2


*---------------------- Table 2 and A3  -------------------*


clear
****************************************************************************
import excel "$dir_outputs/time_series_September_2023_to_be_released.xlsx", sheet("Sheet1") firstrow clear
****************************************************************************


gen period="1985-90" if year_group==1
replace period="1990-93" if year_group==2
replace period="1995-99" if year_group==4
replace period="2000-05" if year_group==5
drop if period==""


gen age_93 = age_wgt-(year-1993)
lab var age_93 "average age in 1993" 

* convert from 2016 to 1993 $:
replace rtirc=rtirc/1.5
replace rse_tirc=rse_tirc/1.5
replace rclkgxi=rclkgxi/1.5
replace rse_clkgxi_wgt=rse_clkgxi_wgt/1.5

* calculate percentages
replace pos_clkgxi_wgt=pos_clkgxi_wgt*100
replace se_pos_clkgxi_wgt=se_pos_clkgxi_wgt*100


lab var age_wgt "average age" 
lab var married "share married"
lab var tnkid "number of children"
lab var rtirc "gross income in 1993$"
lab var rclkgxi "capital gains in 1993$"
lab var log_clkgxi "log(capital gains in 1993$)"
lab var pos_clkgxi_wgt "percent with positive CGs"
lab var n_round  "number of observations"


* individuals with very high incomes not included
drop if cg_8593==1 

* keep variables included in the Summary Stats table
keep   age_wgt  married  tnkidi  rtirc rse_tirc rclkgxi rse_clkgxi_wgt pos_clkgxi_wgt se_pos_clkgxi_wgt  rggex  rse_ggex  rklpyci   rse_klpyci_wgt n_round cg_8593 period n_round
order period cg_8593 age_wgt  married  tnkidi  rtirc rse_tirc rclkgxi rse_clkgxi_wgt pos_clkgxi_wgt se_pos_clkgxi_wgt  rggex  rse_ggex  rklpyci   rse_klpyci_wgt n_round   n_round

foreach var in  married   tnkidi     pos_clkgxi_wgt {
replace `var'=round(`var', 0.01)
}

foreach var in     se_pos_clkgxi_wgt{
replace `var'=round(`var', 0.0001)
}

foreach var in age_wgt rse_clkgxi_wgt   rtirc  rclkgxi rse_tirc  {
replace `var'=round(`var', 1)
}


foreach var in   rse_tirc      se_pos_clkgxi_wgt rse_klpyci_wgt rse_ggex {
tostring `var', replace force
replace `var' = "("+`var'+")"
}

foreach var in   period cg_8593 age_wgt married tnkidi  rtirc  rclkgxi pos_clkgxi_wgt rse_clkgxi_wgt rklpyci rggex_i  n_round {
tostring `var', replace force
}


sort  period cg_8593
sxpose,  clear 
save "$dir_graphs/Table_2_and_A3.dta", replace 




*---------------------- Figure 1  (EMTR different countries)  -------------------*

clear
cap import excel "$dir_outputs/Capital_gains_MTRs_Other_countries.xlsx", sheet("Sheet1") firstrow

rename D CanadaACGTRTreat
rename C CanadaACGTRControl


twoway (connected USAMaximumCapGainsTaxRate Year, mcolor(gs4) msize(med) msymbol(square) lcolor(gs4) lwidth(med) lpattern(solid)) ///
 (connected UKMaximumCapGainsTaxRate Year, mcolor(gs8) msize(med) msymbol(triangle) lcolor(gs8) lwidth(med) lpattern(solid)) ///
 (connected Australia Year, mcolor(white) msize(med) msymbol(diamond) mfcolor(white) mlcolor(black) mlwidth(med) lcolor(black) lwidth(medthick) lpattern(solid)) ///
 (connected CanadaACGTRTreat Year, mlcolor(black) msize(med) msymbol(circle)  lwidth(med) lpattern(solid)) ///
 (connected CanadaACGTRControl Year,  mlcolor(black) msize(med) msymbol(Oh)  lwidth(med) lpattern(solid)) ///
 , xlabel( 1982 1985  1990  1994   2000 2005 2010 2015) ytitle("capital gains tax rate") ytitle(, size(small)) ylabel(0(0.05).65, labsize(small)) xtitle("year") xtitle(, size(small))  ///
 legend(order(4 "Canada EMTR Treated" 5 "Canada EMTR Control" 1 "USA EMTR" 2 "UK EMTR" 3 "Australia EMTR") size(small) pos(1) ring(0) cols(1) row(5)) graphregion(fcolor(white))  xline(1985 1994, lpattern(-) ) 

graph export "$dir_graphs/figure_1.pdf", replace 
 


*---------------------- Figure 2 (lifetime Gains)   -------------------*


clear

cap import excel "$dir_outputs/RESTAT_Main_Text_for_release.xlsx", sheet("Figure2") firstrow


* everything exported in 1982$
* to convert from 1982 to 2016$  -- multiply by 1.6378
* to convert from 2016 into 1993$ -- divide by 1.5
* 

gen rtotal_sum=sum(rtotal)
gen ratio_ever_pos_clkgxi=rever_pos_clkgxi/rtotal*100
gen ratio_ever_pos_clkgxi1k=rever_pos_clkgxi1k/rtotal*100

replace rcsum_clkgxi=rcsum_clkgxi/1000*1.6378/1.5
replace rcsum_clkgxi_p75=rcsum_clkgxi_p75/1000*1.6378/1.5
replace rcsum_clkgxi_p99=rcsum_clkgxi_p99/1000*1.6378/1.5

replace year=year-1949



twoway connected ratio_ever_pos_clkgxi year, msize(1.5)  || connected ratio_ever_pos_clkgxi1k year, msymbol(Oh) msize(1.5)   graphregion(color(white)) bgcolor(white) ytitle("percent of individuals") xtitle("average age") legend(order(1 "% Ever Reported Capital Gains" 2 "% Ever Reported Capital Gains > 1.5K (1993 $)"   ) pos(11) ring(0) col(1))
graph export  "$dir_graphs/figure_2a.pdf", replace


twoway connected rcsum_clkgxi year, yaxis(1) ytitle("thousands of 1993 $") xtitle("average age")  msize(1.5)  ||  connected rcsum_clkgxi_p75 year , yaxis(1) msymbol(Oh) ylabel(0(10)60 ,ang(h)) msize(1.5)  ||  connected rcsum_clkgxi_p99 year, yaxis(2) msymbol(X) ylabel(0(100)600,ang(h) axis(2))  ytitle("thousands of 1993 $", axis(2)) msize(2) graphregion(color(white)) bgcolor(white) legend(order(1 "Average (left axis)" 2 "75th percentile (left axis)" 3 "99th percentile (right axis)"   ) pos(11) ring(0) col(1)) 
graph export  "$dir_graphs/figure_2b.pdf", replace

*---------------------- Figure 3 (hypothetical MTR illustration)   -------------------*

clear
set obs 10
gen year = 1990 in 1
replace year = year[_n - 1] + 1 if year == .


gen x = runiform()
replace x = x - 0.5
replace x = x/100

gen control_1998 = 0.32
gen treat_high_1998 = 0.29
gen treat_low_1998 = 0.26 if year >= 1994
replace treat_low_1998 = 0 if year < 1994

gen control_1995 = 0.32
gen treat_high_1995 = 0.29 if year >= 1994
replace treat_high_1995 = 0 if year < 1994
gen treat_low_1995 = 0.26 if year >= 1994
replace treat_low_1995 = 0 if year < 1994

gen control_1993 = 0.32
gen treat_high_1993 = 0
gen treat_low_1993 = 0


* Figure : Hypothetical estimated future 1995 tax rates by year
twoway (connected control_1995 year if year<=1995, mcolor(black) msize(med) msymbol(circle) lcolor(black) lwidth(med) lpattern(solid)) ///
(connected treat_high_1995 year if year<=1995, mcolor(gs8) msize(med) msymbol(square) lcolor(gs8) lwidth(med) lpattern(solid)) ///
(connected treat_low_1995 year if year<=1995, mcolor(gs4) msize(med) msymbol(triangle) lcolor(gs4) lwidth(med) lpattern(solid)), ///
ytitle("hypothetical estimated 1995 EMTR") ytitle(, size(small)) ylabel(0(0.05).4, labsize(small)) xtitle("year") xtitle(, size(small)) ///
xline(1994, lpattern(-) ) xlabel(1990(2)2000, labsize(small)) ///
legend(order(1 "Control - LCGE to Be Exhausted  in 1992" 2 "Treated - LCGE to Be Exhausted in 1996" 3 "Treated - LCGE Never to Be Exhausted")pos(5) ring(0) cols(1) row(6)  size(small)) ///
graphregion(fcolor(white))

graph export "$dir_graphs/figure_3a.pdf", replace


* Figure : Hypothetical estimated future 1998 tax rates by year
twoway (connected control_1998 year if year<=1998, mcolor(black) msize(med) msymbol(circle) lcolor(black) lwidth(med) lpattern(solid)) ///
(connected treat_high_1998 year if year<=1998, mcolor(gs8) msize(med) msymbol(square) lcolor(gs8) lwidth(med) lpattern(solid)) ///
(connected treat_low_1998 year if year<=1998, mcolor(gs4) msize(med) msymbol(triangle) lcolor(gs4) lwidth(med) lpattern(solid)), ///
ytitle("hypothetical estimated 1998 EMTR") ytitle(, size(small)) ylabel(0(0.05).4, labsize(small)) xtitle("year") xtitle(, size(small)) ///
xline(1994, lpattern(-)) xlabel(1990(2)2000, labsize(small)) ///
legend(order(1 "Control - LCGE to Be Exhausted in 1992" 2 "Treated - LCGE to Be Exhausted in 1996" 3 "Treated - LCGE Never to Be Exhausted" ) pos(5) ring(0) cols(1) row(6) size(small)) ///
graphregion(fcolor(white))
graph export "$dir_graphs/figure_3b.pdf", replace 



*---------------------- Figure 4 (share of CG received)   -------------------*



clear
cap import excel "$dir_outputs/RESTAT_Main_Text_for_release.xlsx", sheet("Figure4") firstrow


lab var share_clkgxi_group_year "share of total reported capital gains"

drop if year==.
reshape wide total_clkgxi_group_year_round total_clkgxi_year_round share_clkgxi_group_year  , i(year) j(group)
gen share_clkgxi_group_year67= share_clkgxi_group_year6+share_clkgxi_group_year7
gen share_clkgxi_group_year25= share_clkgxi_group_year2+share_clkgxi_group_year3 +share_clkgxi_group_year4 +share_clkgxi_group_year5
reshape long total_clkgxi_group_year_round total_clkgxi_year_round share_clkgxi_group_year  , i(year) j(group)




twoway connected  share year if group==2,  ylabel(, ang(h)) lpattern(-)  msymbol(Oh) msize(1.75)|| connected  share year if group==3,  msymbol(O) msize(1.75) || connected  share year if group==4, msymbol(T) msize(1.75) || connected  share year if group==5,  msymbol(S)  msize(1.75)  msymbol(D) xline(1985 1994, lpattern(-) )    graphregion(color(white)) bgcolor(white) xlabel( 1982 1985  1990  1994   2000 2005 2010 2015) legend(order(1 "100K-133K" 2 "67K-100K" 3 "33.33K-67K" 4 "17K-33K"   ) ring(1) pos(6) col(4))
graph export  "$dir_graphs/figure_4a.pdf", replace




twoway connected  share year if group==1, ylabel(, ang(h)) lpattern(-.-)  msymbol(Oh) msize(1.75) || connected  share year if group==25,   ylabel(, ang(h)) mcolor(black)  msymbol(O) msize(1.75) || connected  share year if group==67,  lpattern(-)  msymbol(S) msize(1.75) xline(1985 1994, lpattern(-))   graphregion(color(white)) bgcolor(white) xlabel( 1982 1985  1990  1994   2000 2005 2010 2015) legend(order( 2 "17K-133K (our treat+control)" 1 ">133K (excluded)" 3 "<17K (excluded)"  ) ring(1) pos(6) col(3))
graph export  "$dir_graphs/figure_4b.pdf", replace




*---------------------- Figure 5 (capital gains treat/control time series)   -------------------*


clear
****************************************************************************
cap import excel "$dir_outputs/RESTAT_Main_Text_for_release.xlsx", sheet("Figure5") firstrow
****************************************************************************

lab var clkgxi_dummy_wgt "percent with positive CGs"
replace clkgxi_dummy_wgt=clkgxi_dummy_wgt*100


local var   clkgxi_dummy_wgt 

twoway connected  `var' year if year>=1990 & year<=1999 & cg_8593==2,  ylabel(, ang(h)) lpattern(-)  msymbol(Oh) msize(1.75)|| connected  `var' year if year>=1990 &  year<=1999 & cg_8593==3,   msymbol(O) msize(1.75) || connected  `var' year if year>=1990 &  year<=1999 & cg_8593==4, msymbol(T) msize(1.75) || connected  `var' year if year>=1990 &  year<=1999 &  cg_8593==5,  msymbol(S)  msize(1.75)  msymbol(D) xline(1994 ,lpatter(-))   graphregion(color(white)) bgcolor(white) xlabel(  1990 1992  1994 1996 1998  2000) legend(order(1 "100K-133K" 2 "67K-100K" 3 "33.33K-67K" 4 "17K-33K"   ) ring(1) pos(6) col(4))
graph export  "$dir_graphs/figure_5a.pdf", replace



lab var log_clkgxi_wgt "log(capital gains in 1993$)"
replace log_clkgxi_wgt=log_clkgxi_wgt-log(1.5) 
 

local var log_clkgxi_wgt  

twoway connected  `var' year if year>=1990 & year<=1999 & cg_8593==2,  ylabel(, ang(h)) lpattern(-)  msymbol(Oh) msize(1.75)|| connected  `var' year if year>=1990 &  year<=1999 & cg_8593==3,   msymbol(O) msize(1.75) || connected  `var' year if year>=1990 &  year<=1999 & cg_8593==4,  msymbol(T) msize(1.75) || connected  `var' year if year>=1990 &  year<=1999 &  cg_8593==5,  msymbol(S)  msize(1.75)  msymbol(D) xline(1994 ,   lpatter(-))   graphregion(color(white)) bgcolor(white) xlabel(  1990 1992  1994 1996 1998  2000) legend(order(1 "100K-133K" 2 "67K-100K" 3 "33.33K-67K" 4 "17K-33K"   )ring(1) pos(6) col(4))
graph export  "$dir_graphs/figure_5b.pdf", replace






*---------------------- Figure 6A (main extensive)    -------------------*


clear
****************************************************************************
cap import excel "$dir_outputs/RESTAT_Main_Text_for_release.xlsx", sheet("Figure6a") firstrow
****************************************************************************



foreach cg_8593_2 in 3 4 5  {


if `cg_8593_2'==5{
local title  "Treatment group: 17K-33K  CG in 1985-1993 (1993$)"
}
if `cg_8593_2'==4{
local title  "Treatment group: 33K-67K  CG in 1985-1993 (1993$)"
}
if `cg_8593_2'==3{
local title  "Treatment group: 67K-100K  CG in 1985-1993 (1993$)"
}
local version 2
	twoway connected b year if cg_8593_2==`cg_8593_2'   & version==`version', yline(0, lcolor(gray)) msize(1.5) mcolor(black) title("`title'") ytitle("coefficient estimates ({&Delta} probability)")  ylabel(-0.05(0.025)0.125, ang(h)) || line lb year if cg_8593_2==`cg_8593_2'  & version==`version', lcolor(gray) lpattern(-) || line ub year if cg_8593_2==`cg_8593_2'  & version==`version', lcolor(gray) lpattern(-)  xline(1994, lpattern(-)) graphregion(color(white)) bgcolor(white) xlabel(  1990 1992 1994 1996 1998  2000) legend(off)  xtitle("")
	graph export  "$dir_graphs/figure_6a_cg`cg_8593_2'.pdf", replace
}
	
	
	

*---------------------- Figure 6b (main intensive)    -------------------*


clear
****************************************************************************
cap import excel "$dir_outputs/RESTAT_Main_Text_for_release.xlsx", sheet("Figure6b") firstrow
****************************************************************************


	
	foreach cg_8593_2 in 3 4 5  {


if `cg_8593_2'==5{
local title  "Treatment group: 17K-33K  CG in 1985-1993 (1993$)"
}
if `cg_8593_2'==4{
local title  "Treatment group: 33K-67K  CG in 1985-1993 (1993$)"
}
if `cg_8593_2'==3{
local title  "Treatment group: 67K-100K  CG in 1985-1993 (1993$)"
}

local version 2
	twoway connected b year if cg_8593_2==`cg_8593_2'   & version==`version', yline(0, lcolor(gray))  msize(1.5) mcolor(black) title("`title'") ytitle("coefficient estimates ({&Delta} log(CG))")  ylabel(-0.25(0.25)1.25, ang(h)) || line lb year if cg_8593_2==`cg_8593_2'  & version==`version', lcolor(gray) lpattern(-) || line ub year if cg_8593_2==`cg_8593_2'  & version==`version', lcolor(gray) lpattern(-)  xline(1994, lpattern(-)) graphregion(color(white)) bgcolor(white) xlabel(  1990 1992 1994 1996 1998  2000) legend(off)  xtitle("")
	graph export  "$dir_graphs/figure_6b_cg`cg_8593_2'.pdf", replace
}




*---------------------- Figure 7A (extensive 65+)   -------------------*


*

clear
****************************************************************************
cap import excel "$dir_outputs/RESTAT_Main_Text_for_release.xlsx", sheet("Figure7a") firstrow
****************************************************************************


foreach cg_8593_2 in 3 4 5  {

if `cg_8593_2'==5{
local title  "Treatment group: 17K-33K  CG in 1985-1993 (1993$)"
}
if `cg_8593_2'==4{
local title  "Treatment group: 33K-67K  CG in 1985-1993 (1993$)"
}
if `cg_8593_2'==3{
local title  "Treatment group: 67K-100K  CG in 1985-1993 (1993$)"
}

local version 4
	twoway connected b year if cg_8593_2==`cg_8593_2'   & version==`version', msize(1.5) mcolor(black) yline(0, lcolor(gray)) title("`title'") ytitle("coefficient estimates ({&Delta} probability)") || line lb year if cg_8593_2==`cg_8593_2'  & version==`version', lcolor(gray) lpattern(-) || line ub year if cg_8593_2==`cg_8593_2'  & version==`version', lcolor(gray) lpattern(-)  xline(1994, lpattern(-)) graphregion(color(white)) bgcolor(white) xlabel(  1990 1992 1994 1996 1998  2000) legend(off)  xtitle("")
	graph export  "$dir_graphs/figure_7a_cg`cg_8593_2'.pdf", replace
	
		
}


*---------------------- Figure 7B (intensive 65+ )   -------------------*


clear
****************************************************************************
cap import excel "$dir_outputs/RESTAT_Main_Text_for_release.xlsx", sheet("Figure7b") firstrow
****************************************************************************


foreach cg_8593_2 in 3 4 5  {

if `cg_8593_2'==5{
local title  "Treatment group: 17K-33K  CG in 1985-1993 (1993$)"
}
if `cg_8593_2'==4{
local title  "Treatment group: 33K-67K  CG in 1985-1993 (1993$)"
}
if `cg_8593_2'==3{
local title  "Treatment group: 67K-100K  CG in 1985-1993 (1993$)"
}

local version 4
	twoway connected b year if cg_8593_2==`cg_8593_2'   & version==`version', msize(1.5) mcolor(black) yline(0, lcolor(gray)) title("`title'") ytitle("coefficient estimates ({&Delta} log(CG))")  ylabel(-0.25(0.25)1.25, ang(h)) || line lb year if cg_8593_2==`cg_8593_2'  & version==`version', lcolor(gray) lpattern(-) || line ub year if cg_8593_2==`cg_8593_2'  & version==`version', lcolor(gray) lpattern(-)  xline(1994, lpattern(-)) graphregion(color(white)) bgcolor(white) xlabel(  1990 1992 1994 1996 1998  2000) legend(off)  xtitle("")
	graph export  "$dir_graphs/figure_7b_cg`cg_8593_2'.pdf", replace
		

}


*---------------------- Figure 8A (married extensive)   -------------------*




clear
****************************************************************************
 import excel "$dir_outputs/RESTAT_Main_Text_for_release.xlsx", sheet("Figure8a") firstrow
****************************************************************************





	foreach version in  2 3  {
if `version'==4{
local title  "All individuals"
}
if `version'==2{
local title  "Married"
}
if `version'==3{
local title  "Single"
}

	
	twoway connected b year if cg_8593_2=="all"   & version==`version',  msize(1.5) mcolor(black) yline(0, lcolor(gray)) title("`title'") ytitle("coefficient estimates ({&Delta} probability)")  ylabel(-0.05(0.025)0.125, ang(h)) || line lb year if cg_8593_2=="all"  & version==`version', lcolor(gray) lpattern(-) || line ub year if cg_8593_2=="all"  & version==`version', lcolor(gray) lpattern(-)  xline(1994, lpattern(-)) graphregion(color(white)) bgcolor(white) xlabel(  1990 1992 1994 1996 1998  2000) legend(off)  xtitle("")
	graph export  "$dir_graphs/figure_8a_v`version'.pdf", replace
}



*---------------------- Figure 8B (married intensive);  -------------------*



clear
****************************************************************************
cap import excel "$dir_outputs/RESTAT_Main_Text_for_release.xlsx", sheet("Figure8b") firstrow
****************************************************************************



	foreach version in 2 3  {

if `version'==1{
local title  "All individuals"
}
if `version'==2{
local title  "Married"
}
if `version'==3{
local title  "Single"
}

	twoway connected b year if cg_8593_2=="all"  & version==`version',  msize(1.5) mcolor(black) yline(0, lcolor(gray)) title("`title'")  ytitle("coefficient estimates ({&Delta} log(CG))")  ylabel(, ang(h)) || line lb year if cg_8593_2=="all"  & version==`version', lcolor(gray) lpattern(-) || line ub year if cg_8593_2=="all"  & version==`version', lcolor(gray) lpattern(-)  xline(1994, lpattern(-)) graphregion(color(white)) bgcolor(white) xlabel(  1990 1992 1994 1996 1998  2000) legend(off) 
	graph export  "$dir_graphs/figure_8b_v`version'.pdf", replace

}


*---------------------- Figure 9 (1994 realizations)  -------------------*


clear
****************************************************************************
 import excel "$dir_outputs/RESTAT_Main_Text_for_release.xlsx", sheet("Figure9a") firstrow
****************************************************************************

sort year age_group
tempfile temp 
save `temp', replace

clear
****************************************************************************
cap import excel "$dir_outputs/RESTAT_Main_Text_for_release.xlsx", sheet("Figure9b") firstrow
****************************************************************************
sort year age_group
merge year age_group using `temp'
drop _merge
destring rclkgxi_some6, replace

reshape wide rtotal rsome* rclkgxi* rnone1_past6, i(year) j(age_group)

*reshape wide rtotal  rclkgxi* , i(year) j(age_group)

local var rclkgxi_some6
gen `var'100=(`var'1*rsome1_past61+`var'2*rsome1_past62+`var'3*rsome1_past63)/(rsome1_past61+rsome1_past62+rsome1_past63)
local var rclkgxi_none6
gen `var'100=(`var'1*rnone1_past61+`var'2*rnone1_past62+`var'3*rnone1_past63)/(rnone1_past61+rnone1_past62+rnone1_past63)



replace rclkgxi_some6100=rclkgxi_some6100/1.5
replace rclkgxi_none6100=rclkgxi_none6100/1.5
twoway connected rclkgxi_some6100 year if year>=1988 ,    msize(1.75) msymbol(T) xline(1985 1994,  lpattern(-))  xlabel(1982 1985 1990 1994 2000 2005 2010 2015)  || connected rclkgxi_none6100 year if year>=1988  , msymbol(Sh) msize(1.75)   graphregion(color(white)) bgcolor(white) ytitle("average capital gains  (1993 $)") xtitle("") legend(order(1 "some CG in past 6 years" 2 "no CG past 6 years"   ) pos(1) ring(0) col(1))
graph export  "$dir_graphs/figure_9b.pdf", replace


drop rnone1_past6*

foreach var in  rsome1_past  {
foreach var2 in 2 4 6 {
gen `var'`var2'100=`var'`var2'1+`var'`var2'2+`var'`var2'3
}
}
foreach var in   rsome_past {
foreach var2 in 3 5 7 {
gen `var'`var2'100=`var'`var2'1+`var'`var2'2+`var'`var2'3
}
}
foreach var in rsome1   rtotal{
gen `var'100=`var'1+`var'2+`var'3
}
 
keep year *100

foreach var in 2 4 6 {
gen rnone1_past`var'=rsome1100-rsome1_past`var'100
gen rsome1perc_past`var'=rsome1_past`var'100/rsome1100*100
gen rnone1perc_past`var'=rnone1_past`var'/rsome1100*100
gen rsome1_2_past`var'=rsome1_past`var'100/rtotal100*100
gen rnone1_2_past`var'=rnone1_past`var'/rtotal100*100
}
foreach var in 3 5 7{
gen rnone_past`var'= rtotal100-rsome_past`var'100
}

replace rsome1100=rsome1100/rtotal100*100
replace rsome_past3100=rsome_past3100/rtotal*100
replace rsome_past5100=rsome_past5100/rtotal*100
replace rsome_past7100=rsome_past7100/rtotal*100


twoway   connected rsome1_2_past6 year if year>=1988 , msymbol(T) msize(1.75) ylabel(0(2)12) xline(1985 1994,  lpattern(-))  xlabel(1982 1985 1990 1994 2000 2005 2010 2015) || connected rnone1_2_past6 year if year>=1988  , msymbol(Sh) msize(1.75)    graphregion(color(white)) bgcolor(white) ytitle("% individuals with positive net capital gains ") xtitle("") legend(order(  1 "some CG past 6 years " 2 "no CG past 6 years"  ) pos(1) ring(0) col(1)) 
graph export  "$dir_graphs/figure_9a.pdf", replace


